
//
//  MultiplayerGameScene.m
//  MessangerSkeleton
//
//  Created by User-16 on 4/26/14.
//  Copyright (c) 2014 IOTrainingCamp. All rights reserved.
//

#import "MultiplayerGameScene.h"
#import "MessengerSprite.h"
#import "GamePlay.h"

#import "NetworkManager.h"
#import "IntroScene.h"

#import "cocos2d.h"

@interface GameScene ()

@property MessengerSprite *player;
@property CCNode* batchNode;
@property CCNode* background;

- (void)returnToMenu:(id)sender;

@end

@interface MultiplayerGameScene ()

@property MessengerSprite* otherPlayer;
@property NetworkManager* networkManager;

@property id changeStateObserver;
@property BOOL __block  isRunning;
@property BOOL  __block isSliding;

@property(nonatomic) id disconnectedOpponentObserver;

@end

@implementation MultiplayerGameScene

-(id)initWithLevel:(int)level{
    
    NSLog(@"multiplayer init wiuth level: %d", level);
    self = [super initWithLevel:level];
    if (self) {
        
        //find the type of emeny to produce
        if(((GamePlay*)[GamePlay sharedInstance]).characterType == kMessengerEnemy){
        self. otherPlayer = [[MessengerSprite alloc] initWithType:kMessengerSelf];
        }
        else
        {
            self. otherPlayer = [[MessengerSprite alloc] initWithType:kMessengerEnemy];
        }
        
        //configure enemy
        self.otherPlayer.position  = ccp(self.contentSize.width/4,self.contentSize.height/2 + kEnemyYOffset);
        self.otherPlayer.spawnPoint = self.otherPlayer.position;
        self.otherPlayer.physicsBody.collisionType= @"otherPlayer";
        [self.batchNode addChild:self.otherPlayer z:100];
        
        //network setup
        self.networkManager = [NetworkManager sharedInstance];
        
        NSLog(@"adding observer on MultiplayerScene");
        self.disconnectedOpponentObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kNetworkNotificationNameForDisconnectedOpponent object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
            
            [self goBackToIntroScene];
            NSLog(@"disconnecting - MultiplayerGameScene");
        }];
        
        //observer for the other player's behaviour
        self.changeStateObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kNetworkNotificationNameForReceivedData object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
            
            if ( [note.object isKindOfClass:[NSData class]] )
            {
                EnemyInfo enemyInfo;
                [note.object getBytes:&enemyInfo length:sizeof(enemyInfo)];
                
                //    NSLog(@"enemy data received: %ld", (long)[note.object integerValue]);
                //                CharacterState state = [note.object integerValue];
                switch (enemyInfo.state)
                {
                    case kStateSliding:
                        if(! self.isSliding)
                            [self.otherPlayer slide];
                        break;
                    case kStateRunning:
                        [self.otherPlayer run];
                        break;
                    case kStateJumping:
                        [self.otherPlayer jump];
                        break;
                    default: NSLog(@"Unkown char state received");
                        break;
                }
                self.otherPlayer.position = ccp( self.player.position.x + ( - self.player.distance + enemyInfo.position.x), enemyInfo.position.y -25);
                NSLog(@" relative position %f", self.player.position.x - enemyInfo.position.x/350);
            }
            
        }];

    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self.disconnectedOpponentObserver];
    [[NSNotificationCenter defaultCenter] removeObserver:self.changeStateObserver];
    NSLog(@"removing observer on MultiplayerScene");
}

-(void)update:(CCTime)delta
{
    [super update:delta];
    
    EnemyInfo enemyInfo;
    enemyInfo.state = self.player.state;
    enemyInfo.position = ccp(self.player.distance, self.player.position.y);
    [(NetworkManager*)[NetworkManager sharedInstance] sendEnemyInformation:enemyInfo];
    
    
    //self.otherPlayer.position = ccp(enemyInfo.position.x, enemyInfo.position.y - 50);
    //self.otherPlayer.physicsBody.velocity = ccp(self.otherPlayer.currentSpeed.x - self.player.currentSpeed.x, 0);
}

- (void)returnToMenu:(id)sender
{
    [self goBackToIntroScene];
}

- (void)goBackToIntroScene
{
    [self.networkManager endSession];
    self.networkManager.isAlertViewDisplayedEndConnection = NO;
    [[CCDirector sharedDirector] replaceScene:[IntroScene scene]
                               withTransition:[CCTransition transitionPushWithDirection:CCTransitionDirectionRight duration:1.0f]];
}


@end
